<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport"
          content="width=device-width,height=device-height,inital-scale=1.0,maximum-scale=1.0,user-scalable=no;">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no">

    <title>{$mach['merchant']}-快速付款</title>
    <script src="__STATIC_URL__/static/jquery/3.2.1/jquery.min.js"></script>
    <link rel="stylesheet" href="__STATIC_URL__/static/mobile/css/weui.css" />
    <link rel="stylesheet" href="__STATIC_URL__/static/css/pay.css?id=13"/>
    <!--<link rel="stylesheet" href="__STATIC_URL__/static/css/mobile_pay.css?id=4"/>-->
    <script type="text/javascript" src="__STATIC_URL__/static/js/keyboard2.js?v=111"></script>
    <script src="https://res.wx.qq.com/open/js/jweixin-1.2.0.js"></script>
    <script src="__STATIC_URL__/static/js/app.js?v=9"></script>

</head>

<div>
    <div class="zt">
        <div class="zw"></div>
        <div style="width: 100%;" class="header">
            <div class="top">
                <div class="logo"><img src="{$mach['img']}"></div>
                <div class="mallinfo"><span>{$mach['merchant']}</span></div>
            </div>

            <div style="clear:both"></div>
        </div>
        <div class="zw"></div>
        <div class="cardexplain">

            <ul class="round">

                <li class="nob">
                    <table>

                        <tr>
                            <td>
                                <form role="form" id="tab">
                                    <div class="form-group">
                                        <span id="ti">输入金额(元)</span>
                                        <label for="name"><span class="span">￥</span></label>
                                        <input type="text" class="form-control" id="price" name="price"
                                               oninput="if(value.length>5)value=value.slice(0,5)" readonly="readonly">
                                        <!--<input type="hidden" name="openid" id="openid" value="{$openid}"/>-->
                                        <input type="hidden" name="attach" id="attach" value="qrcodezf"/>
                                        <input type="hidden" name="orderid" id="orderid" value=""/>
                                        <input type="hidden" name="sub_openid" id="sub_openid" value="{$openid}"/>

                                    </div>
                                    <div style="width: 100%;clear: left;border-top: solid 1px #ccc">

                                    </div>
                                </form>
                            <td>
                        </tr>
                    </table>


            </ul>
        </div>

        <div class="foots"><span></span></div>

        <div class="foots"><span></span></div>


        <div style="clean:both"></div>
    </div>
</div>
<div id="loadingToast" style="display:none;">
    <div class="weui-mask_transparent"></div>
    <div class="weui-toast">
        <i class="weui-loading weui-icon_toast"></i>
        <p class="weui-toast__content"></p>
    </div>
</div>
<script>
    var pay_cate = "wx_pay";
    var pay_catename = "微信支付";
    $("#card_pay").click(function () {
        if ($('#card_pay').prop('checked') == true) {


            pay_cate = "card_pay";
            pay_catename = "微信会员卡支付";

        } else {

            pay_cate = "wx_pay";
            pay_catename = "微信支付";
        }
        $('.pay_catename').html(pay_catename);
    });
    (function () {

        var input1 = document.getElementById('price');


        new KeyBoard(input1, "", "{$channel}");

        input1.onclick = function () {
            new KeyBoard(input1, "", "{$channel}");
        };
    })();


    function getorder() {
        var id = {$id};
        $.ajax({
            type: "POST",
            url: "{:url('lftpay/getorder2')}",

            cache: false,
            async: false,
            data: {
                id: id
            },


            success: function (data) {

                $('#orderid').val(data);

            }
        });

    }

    function zf() {
        var $loadingToast = $('#loadingToast');
        $loadingToast.fadeOut(100);

        $("#paytoBtn").prop("onclick",null).off("click");
        // return false;
        if (pay_cate == "card_pay") {

            $.ajax({
                type: "POST",

                url: "/lftpay/card_pay/id/{$id}",

                cache: false,
                dataType: "json",
                async: false,
                data: $('#tab').serialize(),

                success: function (data2) {

                    var data=eval("("+data2+")");
                    if(data.return_code=="SUCCESS"){
                        if(data.result_code=="SUCCESS"){

                            var $loadingToast = $('#loadingToast');
                            $loadingToast.fadeOut(100);
                            paysuccess(data.out_trade_no);

                        }else{
                            var $loadingToast = $('#loadingToast');
                            $loadingToast.fadeOut(100);
                            $('#price').val("");
                            $("#paytoBtn").attr("onclick", "sendpay();");

                            $('#paytoBtn').addClass('disabled');
                            $('#{$channel}').addClass('{$channel}').removeClass('{$channel}s');
                            $('#paytoBtn').html('<span class="{$channel}"  id="{$channel}"></span><span class="pay_catename">'+pay_catename+'</span><b></b>');
                            getorder();


                        }
                    }else{
                        getorder();
                        var $loadingToast = $('#loadingToast');
                        $loadingToast.fadeOut(100);
                        $('#price').val("");
                        $("#paytoBtn").attr("onclick", "sendpay();");
                        $('#paytoBtn').addClass('disabled');
                        $('#{$channel}').addClass('{$channel}').removeClass('{$channel}s');
                        $('#paytoBtn').html('<span class="{$channel}"  id="{$channel}"></span><span class="pay_catename">'+pay_catename+'</span><b></b>');
                    }
                }
            });
        } else {

            getorder();
            var price = $('#price').val();
            if (price < 0.01) {
                alert('金额不能小于0.01元');
                var $loadingToast = $('#loadingToast');
                setTimeout(function () {
                    $loadingToast.fadeOut(100);
                }, 200);
                $('#price').val("");
                $("#paytoBtn").attr("onclick", "sendpay();");
                $('#paytoBtn').addClass('disabled');
                $('#{$channel}').addClass('{$channel}').removeClass('{$channel}s');
                $('#paytoBtn').html('<span class="{$channel}"  id="{$channel}"></span><span class="pay_catename">'+pay_catename+'</span><b></b>');
                $('#price').focus();
            } else if (price > 20000) {
                alert('超过最大支付限额');
                var $loadingToast = $('#loadingToast');
                setTimeout(function () {
                    $loadingToast.fadeOut(100);
                }, 200);
                $('#price').val("");
                $("#paytoBtn").attr("onclick", "sendpay();");
                $('#paytoBtn').addClass('disabled');
                $('#{$channel}').addClass('{$channel}').removeClass('{$channel}s');
                $('#paytoBtn').html('<span class="{$channel}"  id="{$channel}"></span><span class="pay_catename">'+pay_catename+'</span><b></b>');
                $('#price').focus();
            } else {

                $.ajax({
                    type: "POST",

                    url: "/lftpay/pay2/id/{$id}",

                    cache: false,
                    dataType: "json",
                    async: false,
                    data: $('#tab').serialize(),

                    success: function (data) {


                        if (data === 1) {
                            alert('网络超时请重新支付');
                            getorder();
                            var $loadingToast = $('#loadingToast');
                            setTimeout(function () {
                                $loadingToast.fadeOut(100);
                            }, 2000);
                            // $('#paytoBtn').attr('onclick', 'zf()');
                            $("#paytoBtn").attr("onclick", "sendpay();");
                            $('#paytoBtn').val('重新支付');

                        } else {
                            if (typeof WeixinJSBridge == "undefined") {
                                if (document.addEventListener) {
                                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                                } else if (document.attachEvent) {
                                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                                }
                            } else {
                                onBridgeReady(data);
                            }
                        }

                    }
                });
            }
        }
    }

    function onBridgeReady(data) {

        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            data,
            function (res) {
                if (res.err_msg === 'get_brand_wcpay_request:ok') {
                    var $loadingToast = $('#loadingToast');
                    setTimeout(function () {
                        $loadingToast.fadeOut(100);
                    }, 100);
                    paysuccess();

                } else if (res.err_msg === 'get_brand_wcpay_request:fail') {
                    var $loadingToast = $('#loadingToast');
                    setTimeout(function () {
                        $loadingToast.fadeOut(100);
                    }, 100);
                    $('#price').val("");
                    $("#paytoBtn").attr("onclick", "sendpay();");
                    $('#paytoBtn').addClass('disabled');
                    $('#{$channel}').addClass('{$channel}').removeClass('{$channel}s');
                    $('#paytoBtn').html('<span class="{$channel}"  id="{$channel}"></span><span class="pay_catename">'+pay_catename+'</span><b></b>');
                    alert(res.err_desc);
                    getorder();

                } else {
                    var $loadingToast = $('#loadingToast');
                    setTimeout(function () {
                        $loadingToast.fadeOut(100);
                    }, 100);
                    $('#price').val("");
                    $("#paytoBtn").attr("onclick", "sendpay();");
                    $('#paytoBtn').addClass('disabled');
                    $('#{$channel}').addClass('{$channel}').removeClass('{$channel}s');
                    $('#paytoBtn').html('<span class="{$channel}"  id="{$channel}"></span><span class="pay_catename">'+pay_catename+'</span><b></b>');

                    getorder();


                }

            });
    }

    function paysuccess(orderid="") {
        if(orderid==""){
            var ordid = $('#orderid').val();}else{
            ordid=orderid;
        }

        $.ajax({
            type: "POST",

            url: "/lftpay/payover/id/{$id}/orderid/" + ordid,

            cache: false,
            dataType: "json",
            async: false,
            data: {},

            success: function (data) {
                if (data === 0) {

                    window.location.replace("/lftpay/orderinfo/id/{$id}/orderid/" + ordid);
                } else {

                    alert('支付失败');
                }
            }


        });

    }

</script>
</body>
</html>